package com.rcp.steve.crm.navigation;

import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import com.rcp.steve.crm.editor.MakeProductEditor;
import com.rcp.steve.crm.editor.PutfileProductEditor;
import de.vogella.jface.treeviewer.model.CountryMakeProductTodoMockModel;
import de.vogella.jface.treeviewer.model.Todo;
import de.vogella.jface.treeviewer.provider.TodoContentProvider;
import de.vogella.jface.treeviewer.provider.TodoLabelProvider;

public class CountryMakeProductView extends ParentNavigation {
	public static final String ID = "com.rcp.steve.crm.navigation.CountryMakeProductView";
	public void createPartControl(Composite parent) {
		viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		viewer.setContentProvider(new TodoContentProvider());
		viewer.setLabelProvider(new TodoLabelProvider());
		viewer.setAutoExpandLevel(3);
		viewer.setInput(new CountryMakeProductTodoMockModel());
		viewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				IStructuredSelection selection = (IStructuredSelection) event
						.getSelection();
				Todo todo = (Todo) selection.getFirstElement();
				if (todo.getUrl() == null || "".equals(todo.getUrl())) {
					return;
				}
				
				System.err.println("todo:url:" + todo.getUrl());
				IWorkbenchWindow window = PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow();
				IWorkbenchPage page = window.getActivePage();
				try {
					if (todo.getAcBizProduct() == null
							|| "make".equals(todo.getAcBizProduct()
									.getProductpublish())) {
						page.openEditor(todo, MakeProductEditor.ID);
					} else if ("waitsend".equals(todo.getAcBizProduct()
							.getProductpublish()) || "finishsend".equals(todo.getAcBizProduct()
									.getProductpublish())) {
						page.openEditor(todo, PutfileProductEditor.ID);
					}
				} catch (PartInitException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
}